﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FishData :IComparer {

    private int FishID;
    private string fishName;
    private string weapon;
    private int weaponAttri;
    private int atk;
    private int def;
    private int hp;
    private int sp;
    private int hit;
    private int dodge;
    private int crit;
    private int anti_crit;
    private int crit_multiple;
    private int crit_derate;
    private int lucky;
    private int injury;
    private int injury_free;
    private int mana;
    private int maxHp;
    private string buff;
    private int skillType;
    private List<BuffData> skillBuff = new List<BuffData>();

    public delegate void OnManaChangedEvent();
    public event OnManaChangedEvent OnManaChanged;

    public delegate void OnHPChangedEvent(int damage);
    public event OnHPChangedEvent OnHPChanged;

    #region 属性
    public void SetMySelf(FishData fd)
    {
        FishID = fd.FishID1;
        fishName = fd.FishName;
        weapon = fd.Weapon;
        weaponAttri = fd.WeaponAttri;
        atk = fd.Atk;
        def = fd.Def;
        hp = fd.Hp;
        sp = fd.Sp;
        hit = fd.Hit;
        dodge = fd.Crit;
        crit = fd.Crit;
        anti_crit = fd.Anti_crit;
        crit_multiple = fd.Crit_multiple;
        crit_derate = fd.Crit_derate;
        lucky = fd.Lucky;
        injury = fd.Injury;
        injury_free = fd.Injury_free;
        mana = fd.Mana;
        maxHp = fd.MaxHp;
        buff = fd.Buff;
        skillType = fd.SkillType;
        skillBuff = fd.SkillBuff;
}


    public string FishName
    {
        get
        {
            return fishName;
        }

        set
        {
            fishName = value;
        }
    }

    public string Weapon
    {
        get
        {
            return weapon;
        }

        set
        {
            weapon = value;
        }
    }

    public int WeaponAttri
    {
        get
        {
            return weaponAttri;
        }

        set
        {
            weaponAttri = value;
        }
    }

    public int Atk
    {
        get
        {
            return atk;
        }

        set
        {
            atk = value;
        }
    }

    public int Def
    {
        get
        {
            return def;
        }

        set
        {
            def = value;
        }
    }

    public int Hp
    {
        get
        {
            return hp;
        }

        set
        {
            hp = value;
        }
    }

    public int Sp
    {
        get
        {
            return sp;
        }

        set
        {
            sp = value;
        }
    }

    public int Hit
    {
        get
        {
            return hit;
        }

        set
        {
            hit = value;
        }
    }

    public int Dodge
    {
        get
        {
            return dodge;
        }

        set
        {
            dodge = value;
        }
    }

    public int Crit
    {
        get
        {
            return crit;
        }

        set
        {
            crit = value;
        }
    }

    public int Anti_crit
    {
        get
        {
            return anti_crit;
        }

        set
        {
            anti_crit = value;
        }
    }

    public int Crit_multiple
    {
        get
        {
            return crit_multiple;
        }

        set
        {
            crit_multiple = value;
        }
    }

    public int Crit_derate
    {
        get
        {
            return crit_derate;
        }

        set
        {
            crit_derate = value;
        }
    }

    public int Lucky
    {
        get
        {
            return lucky;
        }

        set
        {
            lucky = value;
        }
    }

    public int Injury
    {
        get
        {
            return injury;
        }

        set
        {
            injury = value;
        }
    }

    public int Injury_free
    {
        get
        {
            return injury_free;
        }

        set
        {
            injury_free = value;
        }
    }

    public int Mana
    {
        get
        {
            return mana;
        }

        set
        {
            mana = value;
        }
    }

    public int FishID1
    {
        get
        {
            return FishID;
        }

        set
        {
            FishID = value;
        }
    }

    public int MaxHp
    {
        get
        {
            return maxHp;
        }

        set
        {
            maxHp = value;
        }
    }

    public string Buff
    {
        get
        {
            return buff;
        }

        set
        {
            buff = value;
        }
    }

    public int SkillType
    {
        get
        {
            return skillType;
        }

        set
        {
            skillType = value;
        }
    }

    public List<BuffData> SkillBuff
    {
        get
        {
            return skillBuff;
        }

        set
        {
            skillBuff = value;
        }
    }

    public int Compare(object x, object y)
    {
        throw new NotImplementedException();
    }
    #endregion

    //排序方法，暂时没用
    public int CompareTo(FishData fdata)
    {
        if (fdata == null) return 1;
        FishData otherData = new FishData();
        if (sp > otherData.sp) { return 1; }
        else
        {
            if (sp == otherData.sp) { return 0; }
            else { return -1; }
        }
    }

    //生命值变化
    public int HpChange(int damage)
    {
        hp += damage;

        if(hp >= maxHp)
        {
            hp = maxHp;
        }

        if (damage > 0)
        {
            OnHPChanged(damage);
        }
        return hp;
    }

    //魔法变化
    public int ManaChange(int number)
    {
        mana += number;
        if (mana<= 0)
        {
            mana = 0;
        }

        OnManaChanged();
        return mana;
    }
    
    //伤害变化
    public float Injurychange(int iny)
    {
        injury += iny;
        OnManaChanged();
        return injury;
    }

    //速度变化
    public int SpeedChange(int speed)
    {
        sp += speed;
        OnManaChanged();
        return sp;
    }

    public int  Injury_freeChange(int injfree)
    {
        injury_free += injfree;
        OnManaChanged();
        return injury_free;
    }


    //添加BUFF
    public void AddBuff(BuffData bufData)
    {
        if (skillBuff == null)
        {
            skillBuff.Add(bufData);
            BuffEffect(bufData, 0);
            return;
        }

        if (skillBuff.Contains(bufData))
        {
            bufData.Round = 3;
            BuffEffect(bufData, 1);
        }
        else
        {
            skillBuff.Add(bufData);
            BuffEffect(bufData, 0);
        }

    }

    //BUFF的增加与删除
    public void BuffChange(BuffData bufData)
    {
        if (skillBuff == null)
        {
            return;
        }

        if (skillBuff.Contains(bufData))
        {
            if (bufData.Round <= 0)
            {
                skillBuff.Remove(bufData);
                BuffEffect(bufData, 2);
            }
            else
            {
                bufData.Round--;
                BuffEffect(bufData, 1);
            }
        }
        else
        {
            return;
        }

    }

    //operation    0是添加，1是持续，2是删除
    void BuffEffect(BuffData bufData, int operation)
    {
        if (operation == 0)
        {
            switch (bufData.BufType)
            {
                case BuffType.attack:
                    Injurychange(100);
                    break;
                case BuffType.health:
                    HpChange(Mathf.CeilToInt(maxHp / 20));
                    break;
                case BuffType.speed:
                    Injury_freeChange(80);
                    break;
            }
        }
        else if (operation == 1)
        {
            switch (bufData.BufType)
            {
                case BuffType.attack:
                    
                    break;
                case BuffType.health:
                    HpChange(Mathf.CeilToInt(maxHp / 5));
                    break;
                case BuffType.speed:
                    
                    break;
            }
        }
        else if (operation == 2)
        {
            switch (bufData.BufType)
            {
                case BuffType.attack:
                    Injurychange(-100);
                    break;
                case BuffType.health:
                    
                    break;
                case BuffType.speed:
                    Injury_freeChange(-80);
                    break;
            }
        }
    }

}
